﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using FFmpegVideoClip.Configuration;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace FFmpegVideoClip.windows
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : Window
    {
        public SettingWindow()
        {
            InitializeComponent();
        }
        private void DragTitle_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) => this.DragMove();
        public static void SettingShow()
        {
            SettingWindow ts = new ();
            ts.Owner = Application.Current.MainWindow;
            ts.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            ts.ShowDialog();
        }

        private void OpenPath_Button_Click(object sender, RoutedEventArgs e)
        {
            Button open = (Button)sender;

            using (CommonOpenFileDialog dialog = new CommonOpenFileDialog())
            {
                dialog.Title = "选择路径";
                dialog.IsFolderPicker = true;
                dialog.InitialDirectory = string.IsNullOrEmpty(open.DataContext?.ToString()) ? Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) : open.DataContext.ToString();
                dialog.EnsurePathExists = true;
                if (dialog.ShowDialog() == CommonFileDialogResult.Cancel)
                    return;

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(open.Parent); i++)
                {
                    if (VisualTreeHelper.GetChild(open.Parent, i) is TextBox tb)
                    {
                        tb.Text = dialog.FileName;
                        break;
                    }
                }
            }
        }

        private void CloseAndSave_Button_Click(object sender, RoutedEventArgs e)
        {
            Config.Save(this);
            foreach(Fragment f in ClipManage.AllFragmentEnumerable)
                f.CallPropertyChanged(nameof(f.TimeRangeShow));
            this.Close();

        }
    }
}
